﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using MicropleSoft.Common.Library;
using MicropleSoft.WmsServiceLibrary.Exceptions;
using MicropleSoft.WmsServiceLibrary.Models;
using MicropleSoft.WmsServiceLibrary.ServiceFactory;
using MicropleSoft.WmsServiceLibrary.SqlServer.Base;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MicropleSoft.WmsServiceLibrary.DataSets
{
    public class DataTableWrapHandler : BaseServiceSqlServer, IDataTableWrapHandler
    {
        private readonly AbstractServiceFactory _abstractServiceFactory = ServiceFactoryCreator.CreateAbstractServiceFactory();
        public DataTableWrap GetStaticDataTableWrapByItemClass(string classNumber)
        {
            var list =  _abstractServiceFactory.CreateItemClassService().Query(String.Format(" and CLASS_NUMBER= '{0}' ",classNumber));
            if (list.Count == 0)
            {
                throw new AppServerException("没有相关的表头：" + classNumber);
            }
            var dataTableWrap = new DataTableWrap();
            dataTableWrap.FItemClass = list[0];
            string querySql = _abstractServiceFactory.CreateDataTableFieldBuilder().BuildQuerySqlByItemClass(classNumber);
            var database = _abstractServiceFactory.CreateDatabase();
            DataTable dt = database.ExecuteDataSet(CommandType.Text, querySql).Tables[0];
            dt.TableName = classNumber;
            dataTableWrap.FDataTable = dt;
            return dataTableWrap;
        }

        public DataTableWrap GetStaticDataTableWrapByItemClass(string classNumber,string dataTableCondition)
        {
            var list = _abstractServiceFactory.CreateItemClassService().Query(String.Format(" and CLASS_NUMBER= '{0}' ",
                                                                                 classNumber));
            if (list.Count == 0)
            {
                throw new AppServerException("没有相关的表头：" + classNumber);
            }
            var dataTableWrap = new DataTableWrap();
            dataTableWrap.FItemClass = list[0];
            string querySql = _abstractServiceFactory.CreateDataTableFieldBuilder().BuildQuerySqlByItemClass(classNumber);

            querySql += dataTableCondition;

            var database = _abstractServiceFactory.CreateDatabase();
            DataTable dt = database.ExecuteDataSet(CommandType.Text, querySql).Tables[0];
            dt.TableName = classNumber;
            dataTableWrap.FDataTable = dt;
            return dataTableWrap;
        }

        /// <summary>
        /// 保存DataTable
        /// 负责将整个datatable存入数据库即可
        /// </summary>
        /// <param name="dataTableWrap"></param>
        public void SaveStaticDataTable(DataTableWrap dataTableWrap)
        {
            var itemTemplates = _abstractServiceFactory.CreateItemTemplateService().Query(String.Format(" and TEMPLATE_ID= {0} ", dataTableWrap.FItemClass.ClassId));
            var dataTablePersist = _abstractServiceFactory.CreateDataTablePersist();
            var tempTable = dataTableWrap.FDataTable.GetChanges();

            dataTablePersist.SaveDataTable(itemTemplates, tempTable, dataTableWrap.FItemClass.ItemTablename, Constant.BaseDataCommonPrimaryField);

        }
      
    }
}
